package org.dk.service.Impl;

import org.dk.mapper.TbOrderItemMapper;
import org.dk.mapper.TbOrderMapper;
import org.dk.pojo.*;
import org.dk.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;

    @Override
    public void addOrder(List<TbOrder> list, List<TbOrderItem> itemList) {
        tbOrderMapper.addOrder(list);
        tbOrderItemMapper.addOrderItem(itemList);
    }

    @Override
    public List<OrderVo> selectOrder(String username) {
        List<OrderVo> list = new ArrayList<>();
        TbOrderExample e = new TbOrderExample();
        TbOrderExample.Criteria criteria1 = e.createCriteria();
        criteria1.andUserIdEqualTo(username);
        List<TbOrder> orderList = tbOrderMapper.selectByExample(e);
        for (TbOrder order : orderList) {
            OrderVo vo = new OrderVo();
            vo.setOrder(order);
            TbOrderItemExample example = new TbOrderItemExample();
            TbOrderItemExample.Criteria criteria = example.createCriteria();
            criteria.andOrderIdEqualTo(order.getOrderId());
            List<TbOrderItem> orderItems = tbOrderItemMapper.selectByExample(example);
            vo.setOrderItemList(orderItems);
            list.add(vo);
        }
        return list;
    }

    @Override
    public TbOrderItem selectOrderId(String id) {
        return tbOrderItemMapper.selectByPrimaryKey(Long.valueOf(id));
    }

    @Override
    public TbOrder selectAlipayId(Long orderId) {
        return tbOrderMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public String selectBySellerId(String out_trade_no) {
        TbOrderExample e = new TbOrderExample();
        TbOrderExample.Criteria criteria = e.createCriteria();
        criteria.andSellerorderidEqualTo(out_trade_no);
        List<TbOrder> list = tbOrderMapper.selectByExample(e);
        String userId = list.get(0).getUserId();
        return userId;
    }

    @Override
    public void updateOrder(String trade_no, String out_trade_no) {
        tbOrderMapper.updateOrder(trade_no,out_trade_no);
    }

    @Override
    public void updateOrderrefund(String trade_no, String out_trade_no) {
        tbOrderMapper.updateOrderrefund(trade_no,out_trade_no);
    }
}
